package com.wanhe.callring.module.callring;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;

import com.wanhe.base.BaseDialogFragment;
import com.wanhe.base.widget.NoScrollListView;
import com.wanhe.callring.R;
import com.wanhe.callring.module.base.IBasePresenter;
import com.wanhe.callring.module.base.ILoadDataView;

/**
 * Created by XieZaiWei on 2017/6/19.
 */

public class CallRingDialog extends BaseDialogFragment implements ILoadDataView<String>{

	private static final String KEY_TABLE_ID = "tableId";
	private String mTableNum;
//	@Inject
	public IBasePresenter mCallRingPresenter;
	private View view;

	public static CallRingDialog newInstance(String tableId) {
		CallRingDialog fragment = new CallRingDialog();
		Bundle bundle = new Bundle();
		bundle.putString(KEY_TABLE_ID, tableId);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mTableNum = getArguments().getString(KEY_TABLE_ID);
		}
//		DaggerCallRingComponent.builder().build().inject(this);
		mCallRingPresenter = new CallRingPresenter();
		mCallRingPresenter.setView(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		view = inflater.inflate(R.layout.call_ring_dialog, container, false);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		NoScrollListView lv_message = (NoScrollListView) view.findViewById(R.id.lv_message);
		CallRingAdapter callRingAdapter = new CallRingAdapter(getContext());
		lv_message.setAdapter(callRingAdapter);
		lv_message.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				CallRingAdapter.ViewHolder viewHolder = (CallRingAdapter.ViewHolder) view.getTag();
				String message = viewHolder.tv_message.getText().toString();
				mCallRingPresenter.callRing(mTableNum + "号桌", message);
			}
		});
	}

	@Override
	public void loadData(String data) {
		dismiss();
		CallResultDialog.newInstance(data).show(((FragmentActivity)getContext()).getSupportFragmentManager(), "CallResultDialog");
	}

	@Override
	public void loadNoData() {

	}
}
